﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL


    Public Class Milestone
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal milestone_id As Integer, _
            ByVal project_id As Integer, _
            ByVal title As String, _
            ByVal deadline As DateTime, _
            ByVal comments_count As Integer, _
            ByVal completed As Boolean, _
            ByVal completed_on As DateTime, _
            ByVal completer_id As Integer, _
            ByVal responsible_party_id As Integer, _
            ByVal responsible_party_type As String, _
            ByVal wants_notification As Boolean, _
            ByVal created_on As DateTime, _
            ByVal creator_id As Integer)

            Me.milestone_id = milestone_id
            Me.project_id = project_id
            Me.title = title
            Me.deadline = deadline
            Me.comments_count = comments_count
            Me.completed = completed
            Me.completed_on = completed_on
            Me.completer_id = completer_id
            Me.responsible_party_id = responsible_party_id
            Me.responsible_party_type = responsible_party_type
            Me.wants_notification = wants_notification
            Me.created_on = created_on
            Me.creator_id = creator_id

        End Sub

        ''' <summary>
        ''' Returns List(Of Milestone) from Basecamp API
        ''' </summary>
        Public Overloads Shared Function GetMilestones(ByVal project_id As Integer) As List(Of Milestone)
            Return GetMilestones(project_id, MilestoneType.all)
        End Function

        ''' <summary>
        ''' Returns List(Of Milestone) from Basecamp API
        ''' </summary>
        Public Overloads Shared Function GetMilestones(ByVal project_id As Integer, ByVal filter As MilestoneType) As List(Of Milestone)
            Dim sFilter As String = filter.ToString
            If sFilter = "all" Then sFilter = String.Empty
            Return GetMilestoneListFromMilestoneDetailsCollection(BasecampProvider.API.GetMilestones(project_id, sFilter))
        End Function

        ''' <summary>
        ''' Sets a milestone to incomplete in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function CompleteMilestone(ByVal milestone_id As Integer) As Milestone
            Return GetMilestoneFromMilestoneDetails(BasecampProvider.API.CompleteMilestone(milestone_id))
        End Function

        ''' <summary>
        ''' Sets a milestone to incomplete in Basecamp via Basecamp API
        ''' </summary>
        ''' <remarks>Man! The grammer</remarks>
        Public Shared Function UncompleteMilestone(ByVal milestone_id As Integer) As Milestone
            Return GetMilestoneFromMilestoneDetails(BasecampProvider.API.UncompleteMilestone(milestone_id))
        End Function

        ''' <summary>
        ''' Creates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function CreateMilestone(ByVal project_id As Integer, ByVal title As String, ByVal deadline As DateTime, ByVal notify As Boolean) As Milestone
            Return CreateMilestone(project_id, title, deadline, 0, ResponsiblePartyType.person, notify)
        End Function

        ''' <summary>
        ''' Creates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function CreateMilestone(ByVal project_id As Integer, ByVal title As String, ByVal deadline As DateTime, ByVal responsible_party_id As String, ByVal responsible_party_type As ResponsiblePartyType, ByVal notify As Boolean) As Milestone
            Dim msd As MilestoneDetails = BasecampProvider.API.CreateMilestone(project_id, title, deadline, GetResponsiblePartyString(responsible_party_id, responsible_party_type), notify)
            If msd IsNot Nothing Then
                Return GetMilestoneFromMilestoneDetails(msd)
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Updates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateMilestone(ByVal milestone_id As String, ByVal title As String, ByVal deadline As DateTime, ByVal notifiy As Boolean) As Milestone
            Return UpdateMilestone(milestone_id, title, deadline, 0, ResponsiblePartyType.person, notifiy)
        End Function

        ''' <summary>
        ''' Updates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateMilestone(ByVal milestone_id As Integer, ByVal title As String, ByVal deadline As DateTime, ByVal responsible_party_id As Integer, ByVal responsible_party_type As ResponsiblePartyType, ByVal notify As Boolean) As Milestone
            Dim msd As MilestoneDetails = BasecampProvider.API.UpdateMilestone(milestone_id, title, deadline, GetResponsiblePartyString(responsible_party_id, responsible_party_type), notify)
            If msd IsNot Nothing Then
                Return GetMilestoneFromMilestoneDetails(msd)
            Else
                Return Nothing
            End If

        End Function

        ''' <summary>
        ''' Updates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateMilestone(ByVal milestone_id As Integer, ByVal title As String, ByVal deadline As DateTime, ByVal responsible_party_id As Integer, _
                ByVal responsible_party_type As ResponsiblePartyType, ByVal notify As Boolean, ByVal move_upcoming_milestones As Boolean, ByVal move_upcoming_milesones_off_weekends As Boolean) As Milestone
            Dim msd As MilestoneDetails = BasecampProvider.API.UpdateMilestone(milestone_id, title, deadline, GetResponsiblePartyString(responsible_party_id, responsible_party_type), notify, move_upcoming_milestones, move_upcoming_milesones_off_weekends)
            If msd IsNot Nothing Then
                Return GetMilestoneFromMilestoneDetails(msd)
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Updates a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Function Update() As Milestone
            Dim rp As ResponsiblePartyType = ResponsiblePartyType.person
            If Me.responsible_party_type.ToLower = "company" Then rp = ResponsiblePartyType.company
            Return UpdateMilestone(Me.milestone_id, Me.title, Me.deadline, Me.responsible_party_id, rp, False)
        End Function

        ''' <summary>
        ''' Deletes a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function DeleteMilestone(ByVal milestone_id As Integer) As Milestone
            Dim msd As MilestoneDetails = BasecampProvider.API.DeleteMilestone(milestone_id)
            If msd IsNot Nothing Then
                Return GetMilestoneFromMilestoneDetails(msd)
            Else
                Return Nothing
            End If
        End Function

        ''' <summary>
        ''' Deletes a milestone in Basecamp via Basecamp API
        ''' </summary>
        Public Function Delete() As Milestone
            Return DeleteMilestone(Me.milestone_id)
        End Function

#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of Milestone) from List(Of MilestoneDetails)
        ''' </summary>
        Private Shared Function GetMilestoneListFromMilestoneDetailsCollection(ByVal recordset As List(Of MilestoneDetails)) As List(Of Milestone)
            Dim milestones As New List(Of Milestone)
            Dim record As MilestoneDetails = Nothing
            For Each record In recordset
                milestones.Add(GetMilestoneFromMilestoneDetails(record))
            Next
            Return milestones
        End Function

        ''' <summary>
        ''' Helper Function to create a Milestone from MilestoneDetails
        ''' </summary>
        Private Shared Function GetMilestoneFromMilestoneDetails(ByVal record As MilestoneDetails) As Milestone
            Return New Milestone(record.milestone_id, record.project_id, record.title, record.deadline, record.comments_count, record.completed, record.completed_on, _
                record.completer_id, record.responsible_party_id, record.responsible_party_type, record.wants_notification, record.created_on, record.creator_id)
        End Function

#End Region

#Region "Properties"

        Private _milestone_id As Integer
        Public Property milestone_id() As Integer
            Get
                Return _milestone_id
            End Get
            Set(ByVal value As Integer)
                _milestone_id = value
            End Set
        End Property

        Private _project_id As Integer
        Public Property project_id() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property

        Private _title As String
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Private _deadline As DateTime
        Public Property deadline() As DateTime
            Get
                Return _deadline
            End Get
            Set(ByVal value As DateTime)
                _deadline = value
            End Set
        End Property


        Private _comments_count As Integer
        Public Property comments_count() As Integer
            Get
                Return _comments_count
            End Get
            Set(ByVal value As Integer)
                _comments_count = value
            End Set
        End Property

        Private _completed As Boolean
        Public Property completed() As Boolean
            Get
                Return _completed
            End Get
            Set(ByVal value As Boolean)
                _completed = value
            End Set
        End Property

        Private _completed_on As DateTime
        Public Property completed_on() As DateTime
            Get
                Return _completed_on
            End Get
            Set(ByVal value As DateTime)
                _completed_on = value
            End Set
        End Property

        Private _completer_id As Integer
        Public Property completer_id() As Integer
            Get
                Return _completer_id
            End Get
            Set(ByVal value As Integer)
                _completer_id = value
            End Set
        End Property

        Private _responsible_party_id As Integer
        Public Property responsible_party_id() As Integer
            Get
                Return _responsible_party_id
            End Get
            Set(ByVal value As Integer)
                _responsible_party_id = value
            End Set
        End Property

        Private _responsible_party_type As String
        Public Property responsible_party_type() As String
            Get
                Return _responsible_party_type
            End Get
            Set(ByVal value As String)
                _responsible_party_type = value
            End Set
        End Property

        Private _wants_notification As Boolean
        Public Property wants_notification() As Boolean
            Get
                Return _wants_notification
            End Get
            Set(ByVal value As Boolean)
                _wants_notification = value
            End Set
        End Property

        Private _created_on As DateTime
        Public Property created_on() As DateTime
            Get
                Return _created_on
            End Get
            Set(ByVal value As DateTime)
                _created_on = value
            End Set
        End Property

        Private _creator_id As Integer
        Public Property creator_id() As Integer
            Get
                Return _creator_id
            End Get
            Set(ByVal value As Integer)
                _creator_id = value
            End Set
        End Property


#End Region
    End Class

End Namespace

